home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / text / tex / EVPaths140.lha / TeXSaveString.c < prev    next >
C/C++ Source or Header  |  1995-05-05  |  3KB  |  163 lines

  1. /*
  2.  * TeXSaveString.c - a simple file to show how EVPaths.lib routines work.
  3.  *
  4.  * This program do not use any startup code.
  5.  *
  6.  * Copyright © 1994, 1995 by Giuseppe Ghibò
  7.  *
  8.  * Version 1.1 - 5 May 1995
  9.  *
  10.  * ----------------------------------------------------------------------
  11.  *
  12.  * Compile with:
  13.  * 
  14.  *    SC OPT LINK LIB EVPaths.lib NOSTKCHK NOSTARTUP TeXSaveString.c
  15.  * or
  16.  *    SC OPT NOSTKCHK TeXSaveString.c
  17.  *    SLINK FROM TeXSaveString.o TO TeXSaveString LIB EVPaths.lib LIB:sc.lib
  18.  * or
  19.  *    SC OPT NOSTKCHK NOSTARTUP LINK TeXSaveString.c EVPaths.c SNPrintf.a
  20.  *
  21.  *
  22.  * Usage:
  23.  *
  24.  *    TeXSaveString VAR=MYVAR FILE=<filename> [SHOW] STRING=<string>
  25.  *
  26.  * For example, with:
  27.  *
  28.  *    SetEnv NAME=GOOFY STRING=ram:,T:
  29.  *    TeXSaveString VAR=GOOFY FILE=one.txt STRING=example
  30.  *
  31.  * you'll obtain:
  32.  *
  33.  *    Written string `example' in the file: `one.txt
  34.  *
  35.  * Then type:
  36.  *
  37.  *    Protect ram:one.txt -d
  38.  *
  39.  * and again:
  40.  *
  41.  *    TeXSaveString VAR=GOOFY FILE=one.txt STRING=example SHOW
  42.  *
  43.  * then you'll obtain:
  44.  *
  45.  *    Written string `example' in the file: `T:one.txt'
  46.  *
  47.  */
  48.  
  49. #include <exec/execbase.h>
  50. #include <workbench/startup.h>
  51.  
  52. #include <string.h>
  53.  
  54. #include <proto/dos.h>
  55. #include <proto/exec.h>
  56.  
  57. #include "evpaths.h"
  58.  
  59. #define TEMPLATE "VAR/K,FILE/A,SHOW/S,STRING/F"
  60.  
  61. enum {    OPT_VAR,
  62.     OPT_FILE,
  63.     OPT_SHOW,
  64.     OPT_STRING,
  65.     OPT_COUNT };
  66.  
  67. extern struct ExecBase *SysBase;
  68. extern struct DosLibrary *DOSBase;
  69.  
  70. #define BUFLEN 256
  71. #define EVPBUF 8192L
  72.  
  73. void __saveds Main(void) {
  74.     struct Process *me;
  75.     struct WBStartup *WBenchMsg;
  76.     struct RDArgs *rdargs;
  77.     LONG opts[OPT_COUNT];
  78.     char buf[BUFLEN];
  79.     STRPTR varname = "", fname = NULL, String;
  80.     BOOL show = 0;
  81.     struct EnvVarPath *var;
  82.     BPTR fh;
  83.  
  84.     SysBase = *(struct ExecBase **)4;
  85.  
  86.     if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36L)) == NULL)
  87.         return;
  88.  
  89.     me = (struct Process *)SysBase->ThisTask;
  90.  
  91.     if (me->pr_CLI)
  92.     {
  93.         WBenchMsg = NULL;
  94.  
  95.         memset (opts, 0, sizeof(opts));
  96.  
  97.         if (rdargs = ReadArgs(TEMPLATE, opts, NULL))
  98.         {
  99.             if (opts[OPT_VAR])
  100.                 varname = (STRPTR) opts[OPT_VAR];
  101.  
  102.             if (opts[OPT_FILE])
  103.                 fname = (STRPTR) opts[OPT_FILE];
  104.  
  105.             if (opts[OPT_STRING])
  106.                 String =  (STRPTR) opts[OPT_STRING];
  107.  
  108.             if (opts[OPT_SHOW])
  109.                 show = 1;
  110.  
  111.             var = Alloc_EnvVarPath(varname, EVPBUF);
  112.  
  113.             Init_EnvVarPath(var, ".", ENVPATH_DEFSTR);
  114.  
  115.             if (show)
  116.             {
  117.                 int i = 0;
  118.  
  119.                 Printf("The environment variable `%s' has length %ld\n", varname, GetVarLength(varname));
  120.  
  121.                 while (var->storage.strings[i])
  122.                     Printf("%ld -> `%s'\n", i, var->storage.strings[i++]);
  123.             }
  124.  
  125.             if ((fh = EVP_Open(fname, var, buf, BUFLEN, MODE_NEWFILE)) != DOSFALSE)
  126.             {
  127.                 Write(fh, String, strlen(String));
  128.                 Close(fh);
  129.                 Printf("Written string `%s' in the file: `%s'\n",String,buf);
  130.             }
  131.             else
  132.                 Printf("Can't write in the file `%s'.\n",fname);
  133.  
  134.             Free_EnvVarPath(var);
  135.             FreeArgs(rdargs);
  136.         }
  137.         else
  138.             PrintFault(IoErr(), NULL);
  139.     }
  140.     else
  141.     {
  142.         BPTR StdOut;
  143.  
  144.         WaitPort(&me->pr_MsgPort);
  145.         WBenchMsg = (struct WBStartup *)GetMsg(&me->pr_MsgPort);
  146.  
  147.         if (StdOut = Open("CON:20/20/400/80/A Window/Auto/Close/Wait", MODE_NEWFILE))
  148.         {
  149.             FPrintf(StdOut,"You must run me from CLI, not from WB!\n");
  150.             Close(StdOut);
  151.         }
  152.     }
  153.  
  154.     if (DOSBase)
  155.         CloseLibrary((struct Library *)DOSBase);
  156.  
  157.     if (WBenchMsg)
  158.     {
  159.         Forbid();
  160.         ReplyMsg(&WBenchMsg->sm_Message);
  161.     }
  162. }
  163.